import Axios from 'axios'
import qs from 'qs'
import { baseUrl } from './env'

export default function filter() {
    Axios.defaults.timeout = 10000; //设置请求时间
    Axios.defaults.baseURL = baseUrl;//设置默认接口地址
    Axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
    Axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
    Axios.interceptors.request.use(function (config) {
        // 请求时携带token,如果有的话
        if (localStorage.hasOwnProperty("higame-token")) {
            config.headers['Authorization'] = 'Bearer ' + localStorage.getItem("higame-token");
        }
        if (config.method === 'post') {
            if (config.data == null) {
                config.data = {}
            }
            config.data = qs.stringify(config.data)
        }
        return config
    }, function (error) {
        return Promise.reject(error)
    })
    // respone拦截器
    Axios.interceptors.response.use(function (response) {
        
        return response.data
    }, function (error) {
        return Promise.reject(error.response.data)
    })
}